package com.jianyun.jyzs.widget.calendar;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jianyun.jyzs.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/11/14.
 */

public class BaseCalendar extends LinearLayout{

    private RecyclerView calendarRecycler;
    private List<String> dateList;
    private CalendarAdapter adapter;
    private Calendar baseCalendar;
    private TextView tvMonth;
    private LinearLayout prev;
    private LinearLayout next;

    public BaseCalendar(Context context) {
        super(context);
        initView();
    }

    public BaseCalendar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BaseCalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.calendar_base, null);
        dateList = new ArrayList<>();
        calendarRecycler = (RecyclerView) view.findViewById(R.id.calendar);
        tvMonth = (TextView) view.findViewById(R.id.tv_month);
        prev = (LinearLayout) view.findViewById(R.id.btn_prev_month);
        next = (LinearLayout) view.findViewById(R.id.btn_next_month);
        calendarRecycler.setLayoutManager(new GridLayoutManager(getContext(),7));
        baseCalendar = Calendar.getInstance();
        baseCalendar.setTime(new Date());
        initCalendar(baseCalendar);
        RceLog.d("lml","——————"+dateList.toString());

        initListener();
        addView(view);
    }

    private void initListener() {
        prev.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                baseCalendar.add(Calendar.MONTH, -1);
                initCalendar(baseCalendar);
//                adapter.setList(dateList);
            }
        });
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                baseCalendar.add(Calendar.MONTH, 1);
                initCalendar(baseCalendar);
//                adapter.setList(dateList);
            }
        });
    }

    private void initCalendar(Calendar calendar) {
        dateList.clear();
        int year = calendar.get(Calendar.YEAR);//当前年份
        String year_ = year + "";
        int month = calendar.get(Calendar.MONTH) + 1;//当前月份
        tvMonth.setText(year + "年" + month + "月");//
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获得当前日期所在月份有多少天（或者说day的最大值)
        Calendar calendarClone = (Calendar) calendar.clone(); //克隆一个Calendar再进行操作，避免造成混乱
        calendarClone.set(Calendar.DAY_OF_MONTH, 1);  //将日期调到当前月份的第一天
        int startDayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK); //获得当前日期所在月份的第一天是星期几
        calendarClone.set(Calendar.DAY_OF_MONTH, maxDay); //将日期调到当前月份的最后一天
        int endDayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK); //获得当前日期所在月份的最后一天是星期几
        String month_ = (month + "").length() > 1 ? (month + "") : "0" + month;
        String format = year_ + "-" + month_ + "-";
        for (int i = 0; i < maxDay; i++) {
            int d = i + 1;
            String day_ = (d + "").length() > 1 ? (d + "") : "0" + d;
            //在这里搜索数据
        }
        //计算上一个月在本月日历页出现的那几天.
        //比如，startDayOfWeek = 3，表示当月第一天是星期二，所以日历向前会空出2天的位置，那么让上月的最后两天显示在星期日和星期一的位置上.
        int startEmptyCount = startDayOfWeek - 1; //上月在本月日历页因该出现的天数。
        Calendar preCalendar = (Calendar) calendar.clone();  //克隆一份再操作
        preCalendar.set(Calendar.DAY_OF_MONTH, 1); //将日期调到当月第一天
        preCalendar.add(Calendar.DAY_OF_MONTH, -startEmptyCount); //向前推移startEmptyCount天
        for (int i = 0; i < startEmptyCount; i++) {
            dateList.add(preCalendar.get(Calendar.DAY_OF_MONTH)+""); //将日期添加到数组中
            preCalendar.add(Calendar.DAY_OF_MONTH, 1); //向后推移一天
        }

        /**
         * 计算当月的每一天日期
         */
        calendar.set(Calendar.DAY_OF_MONTH, 1); //由于是获取当月日期信息，所以直接操作当月Calendar即可。将日期调为当月第一天
        for (int i = 0; i < maxDay; i++) {

            dateList.add(calendar.get(Calendar.DAY_OF_MONTH)+"");
            calendar.add(Calendar.DAY_OF_MONTH, 1); //向后推移一天
        }

        /**
         * 计算下月在本月日历页出现的那几天。
         * 比如，endDayOfWeek = 6，表示当月第二天是星期五，所以日历向后会空出1天的位置，那么让下月的第一天显示在星期六的位置上。
         */
        int endEmptyCount = 7 - endDayOfWeek; //下月在本月日历页上因该出现的天数
        Calendar afterCalendar = (Calendar) calendar.clone(); //同样，克隆一份在操作
        for (int i = 0; i < endEmptyCount; i++) {
            dateList.add(afterCalendar.get(Calendar.DAY_OF_MONTH)+"");
            afterCalendar.add(Calendar.DAY_OF_MONTH, 1); //向后推移一天
        }
        this.baseCalendar.add(Calendar.MONTH, -1);

    }

}
